package com.zizo.autohome.user.crawl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.user.bean.Potential;
import com.zizo.autohome.user.dao.PotentialDao;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;

public class PotentialCrawl implements Runnable {
	private int userid;
	private PotentialDao dao;

	public PotentialCrawl(int userid, PotentialDao dao) {
		super();
		this.userid = userid;
		this.dao = dao;
	}

	public void getRz() {
		if (userid % 100000 == 0) {
			System.out.println(userid);
		}
		try {
			String url = "http://i.autohome.com.cn/" + userid + "/car";
			Document doc = DocUtil.getDoc(url);
			if (doc == null) {
				return;
			}
			Elements lis = doc.select("div#dynamic ul.focusCar li");
			List<Integer> rzcids = new ArrayList<>();
			if (lis.size() > 0) {
				for (Element li : lis) {
					if (li.select("a.rzcz").size() > 0 && li.select("a.rzcz").attr("style").length() > 4) {
						rzcids.add(Integer.parseInt(li.select("div.fcpc strong").attr("id").split("_")[2]));
					}
				}
			}
			List<String> list = null;
			if (rzcids.size() > 0) {
				list = getTopic();
			} else {
				return;
			}
			if (list != null && list.size() > 0) {
				for (int rz : rzcids) {
					for (String s : list) {
						String[] ss = s.split("%%");
						Potential p = new Potential(rz, Integer.parseInt(ss[0]), ss[1]);
						dao.insert(p);
					}
				}
			}
		} catch (Exception e) {
			System.out.println(userid);
			e.printStackTrace();
		}
	}

	public List<String> getTopic() {
		String url = "http://i.service.autohome.com.cn/clubapp/OtherTopic-" + userid + "-all-1.html";
		Document doc = DocUtil.getDoc(url);
		if (doc == null) {
			return null;
		}
		int page = 1;
		Elements pages = doc.select("div.paging a:not(.next)");
		if (pages.size() > 0) {
			page = Integer.parseInt(pages.last().text());
		}
		List<String> list = new ArrayList<>();
		Document pagedoc = null;
		for (int i = 1; i < page + 1; i++) {
			if (i == 1) {
				pagedoc = doc;
			} else {
				pagedoc = DocUtil.getDoc(
						"http://i.service.autohome.com.cn/clubapp/OtherTopic-" + userid + "-all-" + i + ".html");
			}
			if (pagedoc != null) {
				Elements trs = pagedoc.select("div.m_t10 table.topicList tr");
				if (trs.size() > 0) {
					trs.remove(0);
					if (trs.size() > 0) {
						for (Element tr : trs) {
							Element t = tr.select("td:not(.txtCen) div.topicTitle p.cp1 a").first();
							Element tt = tr.select("td.txtCen").last();
							String link = t.attr("href");
							String[] links = link.split("-");
							if (links.length > 2) {
								if (links[1].equals("c")) {
									String time = tt.text();
									list.add(Integer.parseInt(links[2]) + "%%" + time);
								}
							}
						}
					}
				} else {
					break;
				}
			}

		}
		return list;
	}

	public void run() {
		getRz();
	}

	public static void main(String[] args) {
		Connection conn = ConnectionPool.getConnection();
		PotentialDao dao = new PotentialDao(conn);
		ExecutorService fix = Executors.newFixedThreadPool(25);
		List<Integer> userids = dao.getIDs();
		for (int uid : userids) {
			fix.execute(new PotentialCrawl(uid, dao));
		}
		fix.shutdown();
	}
}
